package com.ncloud.crm.mapper;

import com.ncloud.crm.entity.order.WorkNodeCloudIpseg;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * Created by shiqiang on 2017/4/17.
 */
@Mapper
public interface WorkPublicCloudIpsegMapper {
    @Select(value = { "select * from bs_o_work_cloud_ipseg where seg_inst_id = #{segInstId}" })
    @Results({ @Result(column = "seg_inst_id", property = "segInstId"), @Result(column = "cloud_id", property = "cloudId"),
			@Result(column = "work_id", property = "workId"), @Result(column = "modify_tag", property = "modifyTag"),
            @Result(column = "vpc_ip_seg", property = "vpcIpSeg"),@Result(column="gateway_ip",property="gatewayIp"),
            @Result(column="begin_date",property="beginDate"),@Result(column="end_date",property="endDate"),
            @Result(column="update_time",property="updateTime"),@Result(column="update_user",property="updateUser")})
    public WorkNodeCloudIpseg getWorkCloudSegByPk(Long segInstId);

    @Insert("insert into bs_o_work_cloud_ipseg(work_id,seg_inst_id,modify_tag, cloud_id, vpc_ip_seg, gateway_ip, begin_date, end_date, update_time, update_user)"
    		+ " values(#{workId},#{segInstId},#{modifyTag}, #{cloudId}, #{vpcIpSeg}, #{gatewayIp}, #{beginDate}, #{endDate}, #{updateTime}, #{updateUser})")
	public int insertWorkCloudSeg(WorkNodeCloudIpseg node);

    @Select(value = { "select * from bs_o_work_cloud_ipseg where work_id = #{workId}" })
    @Results({ @Result(column = "seg_inst_id", property = "segInstId"), @Result(column = "cloud_id", property = "cloudId"),
            @Result(column = "work_id", property = "workId"), @Result(column = "modify_tag", property = "modifyTag"),
            @Result(column = "vpc_ip_seg", property = "vpcIpSeg"),@Result(column="gateway_ip",property="gatewayIp"),
            @Result(column="begin_date",property="beginDate"),@Result(column="end_date",property="endDate"),
            @Result(column="update_time",property="updateTime"),@Result(column="update_user",property="updateUser")})
    public List<WorkNodeCloudIpseg> getWorkCloudSegByWorkId(Long workId);
    
    @Select("select #{workId} as work_id,seg_inst_id,cloud_id, vpc_ip_seg, gateway_ip"
			+ " from bs_cloud_ipseg where cloud_id = #{cloudId} and NOW() BETWEEN begin_date and end_date"
			+ " and seg_inst_id not in ( select seg_inst_id from bs_o_work_cloud_ipseg "
			+ " where work_id = #{workId} and cloud_id = #{cloudId} and modify_tag = '3')"
			+ " UNION "
			+ " select work_id,seg_inst_id,cloud_id, vpc_ip_seg, gateway_ip"
			+ " from bs_o_work_cloud_ipseg where work_id = #{workId} and cloud_id = #{cloudId} and modify_tag='1'")
	@Results({ @Result(column = "seg_inst_id", property = "segInstId"),
			@Result(column = "cloud_id", property = "cloudId"), @Result(column = "work_id", property = "workId"),
			@Result(column = "vpc_ip_seg", property = "vpcIpSeg"),
			@Result(column = "gateway_ip", property = "gatewayIp") })
	public List<WorkNodeCloudIpseg> getNewIpSegsByCloudId(@Param("workId")Long workId, @Param("cloudId")Long cloudId);
}
